package com.uenpay.im.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.im.R;
import com.uenpay.im.entities.ContactsIndexBean;

import java.text.MessageFormat;
import java.util.List;


/**
 * Created by zhangxutong .
 * Date: 16/08/28
 */

public class ContactsIndexAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected Context mContext;
    protected List<ContactsIndexBean> mDatas;
    protected LayoutInflater mInflater;
    private onRecyclewItemClickListener mItemClickListener;

    public ContactsIndexAdapter(Context mContext, List<ContactsIndexBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<ContactsIndexBean> getDatas() {
        return mDatas;
    }

    public ContactsIndexAdapter setDatas(List<ContactsIndexBean> datas) {
        mDatas = datas;
        return this;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ContactsIndexBean.CONTACTS_TYPE_COMMON || viewType == ContactsIndexBean.CONTACTS_TYPE_TOP) {
            return new CommonViewHolder(mInflater.inflate(R.layout.item_contacts_index, parent, false));
        } else if (viewType == ContactsIndexBean.CONTACTS_TYPE_SEARCH) {
            return new SearchViewHolder(mInflater.inflate(R.layout.include_search_layout, parent, false));
        } else if (viewType == ContactsIndexBean.CONTACTS_TYPE_BOTTOM) {
            return new BottomViewHolder(mInflater.inflate(R.layout.item_contacts_bottom, parent, false));
        } else {
            throw new RuntimeException("onCreateViewHolder# the viewholder of the type is null!");
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CommonViewHolder) {
            bindCommonViewData((CommonViewHolder) holder, position);
        } else if (holder instanceof SearchViewHolder) {
            bindSearchViewData((SearchViewHolder) holder, position);
        } else if (holder instanceof BottomViewHolder) {
            bindBottomViewData((BottomViewHolder) holder, position);
        }

    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }


    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getType();
    }

    public onRecyclewItemClickListener getItemClickListener() {
        return mItemClickListener;
    }

    public void setItemClickListener(onRecyclewItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    private void bindCommonViewData(CommonViewHolder holder, final int position) {
        final ContactsIndexBean contactsIndexBean = mDatas.get(position);
        holder.tvNickName.setText(contactsIndexBean.getName());
        if (!contactsIndexBean.isTop()) {
            holder.tvRealName.setVisibility(View.VISIBLE);
            if (null != contactsIndexBean.getFriendEntity() && !TextUtils.isEmpty(contactsIndexBean.getFriendEntity().real_name)) {
                holder.tvRealName.setText(contactsIndexBean.getFriendEntity().real_name);
            } else {
                holder.tvRealName.setText("对方隐藏了真实姓名");
            }
        } else {
            holder.tvRealName.setVisibility(View.GONE);
        }
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(v, position);
                }
            }
        });
        if (contactsIndexBean.getIcon() == 0) {
            holder.ivAvatar.setImageResource(R.drawable.friend);
        } else {
            holder.ivAvatar.setImageResource(contactsIndexBean.getIcon());
        }

        if (contactsIndexBean.getType() == ContactsIndexBean.CONTACTS_TYPE_TOP && contactsIndexBean.getUnreadNum() > 0) {
            holder.tvUnreadNum.setVisibility(View.VISIBLE);
            holder.tvUnreadNum.setText(contactsIndexBean.getUnreadNum() < 100 ? contactsIndexBean.getUnreadNum() + "" : "99+");
        } else {
            holder.tvUnreadNum.setVisibility(View.GONE);
        }
    }

    private void bindSearchViewData(SearchViewHolder holder, final int position) {
        holder.content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(v, position);
                }
            }
        });
    }

    private void bindBottomViewData(BottomViewHolder holder, final int position) {
        holder.content.setText(MessageFormat.format(mContext.getResources().getString(R.string.total_contacts), mDatas.get(position).getContactsNum()));
    }

    public static class CommonViewHolder extends RecyclerView.ViewHolder {
        TextView tvNickName;
        TextView tvRealName;
        ImageView ivAvatar;
        TextView tvUnreadNum;
        View content;

        public CommonViewHolder(View itemView) {
            super(itemView);
            tvNickName = (TextView) itemView.findViewById(R.id.tv_nick_name);
            tvRealName = (TextView) itemView.findViewById(R.id.tv_real_name);
            ivAvatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tvUnreadNum = (TextView) itemView.findViewById(R.id.tv_unread_count);
            content = itemView.findViewById(R.id.content);
        }
    }

    public static class SearchViewHolder extends RecyclerView.ViewHolder {

        View content;

        public SearchViewHolder(View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.content);
        }
    }

    public static class BottomViewHolder extends RecyclerView.ViewHolder {

        TextView content;

        public BottomViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.tv_contacts_num_desc);
        }
    }

    public interface onRecyclewItemClickListener {
        void onItemClick(View view, int position);
    }
}
